<?php

class AuthController extends BaseController
{
    public function googleAction()
    {
        using('Framework.System.Google.Auth');
        $auth = new GoogleAuthorization();
        $auth->login('/auth/googid');
    }

    public function googidAction()
    {
        using('Framework.System.Google.Auth');
        $resp = GoogleAuthorization::getResponse();
        if ($resp->isSuccess()) {
            $email = $resp->getEmail();
            $user = User::getUserByGoogleMail($email);
            if ($user) {
                Session::Singleton()->user = $user;
            }
            Url::redirect('/');
        }
        Url::goBack();
    }

    public function loginAction()
    {
        $user = User::getUserByLoginPassword($_POST['login'], $_POST['password']);
        if ($user) {
            Session::Singleton()->user = $user;
        }
        Url::goBack();
    }

    public function logoutAction()
    {
        Session::Singleton()->user = null;
        Url::goBack();
    }
}